package com.huike.web.controller.clues;

import com.huike.business.service.TbBusinessService;
import com.huike.clues.domain.vo.AssignmentVo;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.service.TbClueService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.alibaba.excel.EasyExcel;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.result.ImportClueData;
import com.huike.clues.utils.easyExcel.ExcelListener;
import com.huike.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 线索管理Controller
 *
 * @date 2023-04-02
 */
@RestController
@RequestMapping("/clues/clue")
@Api(tags = "线索管理相关接口")
public class TbClueController extends BaseController {
    @Resource
    private TbBusinessService tbBusinessService;

    @Resource
    private TbClueService tbClueService;

    @ApiOperation("线索转商机")
    @PreAuthorize("@ss.hasPermi('clues:clue:changeBusiness')")
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult<Integer> changeBusiness(@PathVariable Long id){
        //根据id调用tbBusinessService的changeBusiness方法，将线索转换为商机
        return toAjax(tbBusinessService.changeBusiness(id));
    }

    @ApiOperation("伪线索")
    @PreAuthorize("@ss.hasPermi('clues:clue:false')")
    @PutMapping("/false/{id}")
    public AjaxResult cluesFalse(@PathVariable Long id, @RequestBody FalseClueVo falseClueVo) {
        return toAjax(tbClueService.falseClue(id, falseClueVo.getReason(), falseClueVo.getRemark()));
    }

    @ApiOperation("批量捞取")
    @PreAuthorize("@ss.hasPermi('clues:clue:gain')")
    @PutMapping("/gain")
    public AjaxResult<String> gain(@RequestBody AssignmentVo assignmentVo) {
        // 调用tbClueService的gain方法，传入分配记录请求参数对象的分配的线索id数组和分配给目标用户id
        // 需要返回字符串告知用户是否成功,所以用AjaxResult.success接收
        return AjaxResult.success(tbClueService.gain(assignmentVo.getIds(), assignmentVo.getUserId()));
    }

    /*查询线索管理列表*/
    @ApiOperation("查询线索管理列表")
    @GetMapping("/list")
    public TableDataInfo list(TbClue tbClue){
        startPage();
        List<TbClue> tbClueList = tbClueService.selectTbClueList(tbClue);
        return getDataTable(tbClueList);
    }

    /*
    * 新增线索管理
    *
    * */
    @ApiOperation("新增线索管理")
    @PostMapping
    public AjaxResult addClue(@RequestBody TbClue tbClue){
        //判断手机号是否已经存在 存在返回true
        Boolean flag = tbClueService.selectTbCluePhone(tbClue.getPhone());
        if (flag){
            return error("该手机号已注册");
        }
        tbClueService.addClue(tbClue);
        return AjaxResult.success();
    }

    /*
    * 根据id获取线索基础信息
    * */
    @ApiOperation("根据id获取线索基础信息")
    @PreAuthorize("@ss.hasPermi('clues:clue:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<TbClue> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tbClueService.selectTbClueById(id));
    }

    /*
    * 查询线程池
    * */
    @ApiOperation("查询线程池")
    @GetMapping("/pool")
    public TableDataInfo pool(TbClue tbClue){
        startPage();
        List<TbClue> tbClueList = tbClueService.pool(tbClue);
        return getDataTable(tbClueList);
    }

    /*
    * 修改线索管理
    * */
    @ApiOperation("修改线索管理")
    @PutMapping
    public AjaxResult update(@RequestBody TbClue tbClue){
        tbClueService.updateClue(tbClue);
        return AjaxResult.success();
    }

    /*
    * 批量分配
    * */
    @ApiOperation("批量分配")
    @PutMapping("assignment")
    public AjaxResult assignment(@RequestBody AssignmentVo assignmentVo){
        tbClueService.assignment(assignmentVo);
        return AjaxResult.success();
    }

    @ApiOperation("上传线索")
    @Log(title = "上传线索", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult<ImportClueData> importData(MultipartFile file) throws Exception {
        ExcelListener excelListener = new ExcelListener(tbClueService);
        EasyExcel.read(file.getInputStream(), TbClueExcelVo.class, excelListener).sheet().doRead();
        return AjaxResult.success(excelListener.getResultData());
    }
}
